/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.tukano.odt;

import it.tukano.fun.Tuple2;
import it.tukano.lang.StringExt;
import it.tukano.swingdeco.containers.JPanelExt;
import it.tukano.swingdeco.controls.JCheckBoxExt;
import java.awt.BorderLayout;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;

/**
 *
 * @author utente3
 */
public class OdtOpListItemEditor extends OdtFieldEditor {
    
    /* default serial version code */
    private static final long serialVersionUID = 0;
    
    private final JCheckBoxExt box = new JCheckBoxExt();

    public OdtOpListItemEditor(Integer size, String label, StringExt defaultValue) {
        JPanelExt panel = new JPanelExt(new BorderLayout());
        panel.add(new JLabel(label));
        panel.add(box, BorderLayout.WEST);
        add(panel);
    }
    
    public boolean isSelected() {
        return box.isSelected();
    }

    @Override
    public void setValue(StringExt t) {
        box.setSelected(t.toBoolean(false));
    }

    //TODO fix this to return true/false and auto-transform the xml source code
    @Override
    public Tuple2<StringExt, String> applyEditorValue(OdtField odtField, String xml) {
        if(odtField != null && xml != null && box.isSelected() == false) {
            xml = odtField.eraseWrappingList(xml);
            return Tuple2.newInstance(new StringExt(""), xml);
        }
        return Tuple2.newInstance(StringExt.wrap(""), xml);
    }

    @Override
    public void clearValue() {
    }

    @Override
    public String getStorableStringValue() {
        return String.valueOf(box.isSelected());
    }
}
